package com.csinc.generate.core.common.factory;

import com.baomidou.mybatisplus.plugins.Page;
import com.csinc.generate.core.common.state.Order;
import com.csinc.generate.core.utils.lang.StringUtils;
import com.csinc.generate.core.utils.support.http.HttpUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * BootStrap Table默认的分页参数创建
 * Create By 郭金宝 on 2018/7/26.<br>
 */
public class PageFactory<T> {

    public Page<T> defaultPage() {
        HttpServletRequest request = HttpUtils.getRequest();
        int limit = Integer.valueOf(request.getParameter("limit"));
        int offset = Integer.valueOf(request.getParameter("offset"));
        String sort = request.getParameter("sort");
        String order = request.getParameter("order");
        if (StringUtils.isEmpty(sort)) {
            Page<T> page = new Page<T>((offset / limit + 1), limit);
            page.setOpenSort(false);
            return page;
        } else {
            Page<T> page = new Page<T>((offset / limit + 1), limit, sort);
            if (Order.ASC.getDes().equals(order)) {
                page.setAsc(true);
            } else {
                page.setAsc(false);
            }
            return page;
        }
    }
}